package cn.wx.org.service.impl;


import cn.wx.basic.service.impl.BaseServiceImpl;
import cn.wx.basic.util.PageList;
import cn.wx.org.domain.Department;
import cn.wx.org.mapper.DepartmentMapper;
import cn.wx.org.query.DepartmentQuery;
import cn.wx.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;


    @Transactional
    @Override
    public void save(Department department) {
            //返回自增主键id
        departmentMapper.save(department);

        //department 页面传来的 ，dirPath没有
        //1.没有上级部门的 dirPath= /id
        if(department.getParent_id()==null){
            Long id = department.getId();  //获取添加时返回的id
            department.setDirPath("/"+id);  //设置dirPath
        }else {   //2.有上级部门的
            //获取它的parent_id
            Long parent_id = department.getParent_id();

            //因为上级部门parent_id对应的上级部门是主键id，所以通过他查询
            Department department1 = departmentMapper.loadById(parent_id); //当前的上一级部门

            //拼接dirPath
            department.setDirPath(department1.getDirPath()+"/"+ department.getId());
        }
        // 调用update方法，更新到数据库
        update(department);

    }

    @Transactional
    @Override
    public void update(Department department) {
        if(department.getParent_id()==null){
            Long id = department.getId();  //获取添加时返回的id
            department.setDirPath("/"+id);  //设置dirPath
        }else {   //2.有上级部门的
            //获取它的parent_id
            Long parent_id = department.getParent_id();

            //因为上级部门parent_id对应的上级部门是主键id，所以通过他查询
            Department department1 = departmentMapper.loadById(parent_id); //当前的上一级部门

            //拼接dirPath
            department.setDirPath(department1.getDirPath()+"/"+ department.getId());
        }

        departmentMapper.update(department);
    }



    //获取子集部门对象
    @Override
    public List<Department> deptTree() {
        //获取所有对象
        List<Department> all = departmentMapper.loadAll();

        //创建一个map，通过id  K值 获取 对象
        HashMap<Long, Department> map = new HashMap<>();

        //循环所有对象，存入到map中
        for (Department department : all) {
            map.put(department.getId(),department);
        }
        //前端需要得到的集合数据 - 部门树
        ArrayList<Department> deptTree = new ArrayList<>();

        for (Department department : all) {
            //如果patent_id==null表示为顶级部门
            if(department.getParent_id()==null){
                deptTree.add(department);
            }else {
              Long parent_id = department.getParent_id();  //如果不是，把循环当前的parent_id接收一下
                Department parentDepartment = map.get(parent_id);  //获取map里面当前的循环的谁的parent_id是这个parent_id 的对象
                parentDepartment.getChildren().add(department);  //把这个子集部门封装到Children中

            }
        }
        return deptTree;
    }
}
